﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.SqlClient;

namespace ConsoleApplication1.Thread5
{
    public class Thread5
    {
        static string connectionString = @"server=PEK7-DEV-02.vancloa.cn;database=Vancl_Union;uid=vanclv2;pwd=vanclV2;Asynchronous Processing=true";
        public  static void Main()
        {
            ThreadPool.SetMaxThreads(1000, 1000);
            ThreadPoolMessage("start");
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("WAITFOR DELAY '0:0:3';",con);
            con.Open();

            IAsyncResult res = cmd.BeginExecuteNonQuery();
            ThreadPoolMessage("begin read");
            while(!res.AsyncWaitHandle.WaitOne(500))
            {
                Console.WriteLine("MainTread do work");
            }
            int count = cmd.EndExecuteNonQuery(res);
            ThreadPoolMessage("Complete");
            Console.ReadKey();
        }


        //显示线程池现状31         
        static void ThreadPoolMessage(string data)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string message = string.Format("{0}\n  CurrentThreadId is {1}\n  WorkerThreads is:{2}  CompletionPortThreads is :{3}\n",
                data, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
            Console.WriteLine(message);
        }
    }
}
